package wms

import (
	"context"
	"encoding/json"
	"ez/apps/wms/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Summary struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64      `json:"id" bson:"id,omitempty"`
	Item        *ItemSku   `json:"item" bson:"item"`         //物品
	ItemId      int64      `json:"itemId" bson:"itemId"`     //物品
	ItemName    string     `json:"itemName" bson:"itemName"` //物品名
	Type        *IOType    `json:"type" bson:"type"`         //入库类型
	TypeId      int64      `json:"typeId" bson:"typeId"`     //入库类型
	TypeName    string     `json:"typeName" bson:"typeName"` //类型名称
	IoType      int64      `json:"ioType" bson:"ioType"`     //出入库类型
	Ymd         int64      `json:"ymd" bson:"ymd"`           //年月日
	Y           int64      `json:"y" bson:"y"`               //年
	M           int64      `json:"m" bson:"m"`               //月
	D           int64      `json:"d" bson:"d"`               //日
	Num         float64    `json:"num" bson:"num"`           //数量
	Cost        float64    `json:"cost" bson:"cost"`         //价值
	CreateAt    time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *Summary) DocName() string { return "Summary" }
func (this *Summary) GetId() int64    { return this.Id }
func (this *Summary) SetId(id int64)  { this.Id = id }
func (this *Summary) Create() error {
	return this.GetFactory().Create(this)
}
func (this *Summary) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *Summary) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *Summary) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *Summary) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *Summary) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *Summary) ToString() string {
	return string(this.ToBytes())
}
func (this *Summary) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Summary) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Summary) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *Summary) LoadItem() {
	if this.ItemId == 0 {
		return
	}
	this.Item, _ = NewItemSkuCrud().FindId(this.ItemId)
}
func (this *Summary) LoadType() {
	if this.TypeId == 0 {
		return
	}
	this.Type, _ = NewIOTypeCrud().FindId(this.TypeId)
}
func (this *Summary) ClearRelationsBeforeSave() mgo.Doc {
	this.Item = nil
	this.Type = nil
	return this
}
func neverUsed_Summary() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type SummaryAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *Summary
	Session *ez.Session
}

func NewSummaryAccessControl(model *Summary, action string, session *ez.Session) *SummaryAccessControl {
	ctrl := &SummaryAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.SummaryAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
